<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/6
 * Time: 14:40
 */
namespace App\Http\Controllers\Api\LandingE;

use App\Http\Controllers\Controller;
use App\Repositories\CollectMoneyBillRepository;
use App\Repositories\PersonalAccountWithdrawRepository;
use App\Repositories\RepaymentAccountWithdrawRepository;
use App\Repositories\RepaymentPlanConsumeRepository;
use App\Repositories\RepaymentPlanRepository;
use App\Repositories\ThreeApiErrorLogsRepository;
use App\Repositories\UserCreditCardRepository;
use App\Repositories\UserRepository;
use App\Services\CollectMoneyProfitService;
use App\Services\MessageService;
use App\Services\RepaymentProfitService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class LandingENotifyController extends Controller
{
    protected $threeApiErrorLogsRepository;
    protected $repaymentProfitService;
    protected $repaymentAccountWithdrawRepository;
    protected $personalAccountWithdrawRepository;
    protected $collectMoneyProfitService;
    protected $userRepository;
    protected $userCreditCardRepository;
    protected $messageService;

    public function __construct(ThreeApiErrorLogsRepository $threeApiErrorLogsRepository,
                                RepaymentProfitService $repaymentProfitService,
                                RepaymentAccountWithdrawRepository $repaymentAccountWithdrawRepository,
                                PersonalAccountWithdrawRepository $personalAccountWithdrawRepository,
                                CollectMoneyProfitService $collectMoneyProfitService,
                                UserRepository $userRepository,
                                UserCreditCardRepository $userCreditCardRepository,
                                MessageService $messageService)
    {
        $this->threeApiErrorLogsRepository = $threeApiErrorLogsRepository;
        $this->repaymentProfitService = $repaymentProfitService;
        $this->repaymentAccountWithdrawRepository = $repaymentAccountWithdrawRepository;
        $this->personalAccountWithdrawRepository = $personalAccountWithdrawRepository;
        $this->collectMoneyProfitService = $collectMoneyProfitService;
        $this->userRepository = $userRepository;
        $this->userCreditCardRepository = $userCreditCardRepository;
        $this->messageService = $messageService;
    }

    /**
     * 消费回调
     * {
     * "sign": "2e4fe1fd77b7efa4060a4f3e1eafb1de",
     * "amount": "40.81",
     * "transtype": "85",
     * "merchno": "lmkj25se267f69f",
     * "status": "00",
     * "signType": "MD5",
     * "dsorderid": "AA17870572263812",
     * "orderid": "2018101722400400100154565",
     * "paytime": "20181017224111"
     * }
     * {"sign":"2e4fe1fd77b7efa4060a4f3e1eafb1de","amount":"40.81","transtype":"85","merchno":"lmkj25se267f69f","status":"00","signType":"MD5","dsorderid":"AA17870572263812","orderid":"2018101722400400100154565","paytime":"20181017224111"}
     */
    public function consumeNotify(Request $request)
    {
        Log::info('consumeNotifyE:' . json_encode($request->all()));
        $repaymentPlanConsumeRepository = new RepaymentPlanConsumeRepository();
        $consume = $repaymentPlanConsumeRepository->byConsumeSn($request->dsorderid);
        $repaymentPlan = new RepaymentPlanRepository();
        $plan = $repaymentPlan->byId($consume->repayment_plan_id);
        //更新消费状态
        if ($request->status == '00') {
            echo 'SUCCESS';
            $repaymentPlanConsumeRepository->updateStatusByConsumeSn($request->dsorderid, 2002);//成功
            //发送消费成功消息提醒
            $userOwn = $this->userRepository->byId($plan->user_id);
            $creditCard = $this->userCreditCardRepository->isExistUserBankNo($plan->user_id, $consume->bank_no);
            $this->messageService->messageTemplate(17, $userOwn->invited_code, $creditCard->bank_no, $creditCard->bank_description,
                $consume->consume_money, '', '', $consume->repayment_plan_id, '', '');
            $this->repaymentProfitService->profit($request->dsorderid, $consume->consume_money, $consume->consume_service_charge,
                $consume->consume_passage_id, $plan->user_id, $creditCard->bank_code, $userOwn->mobile);
        } else {
            $message = isset($request->message) ? $request->message : '';
            $repaymentPlanConsumeRepository->updateStatusByConsumeSn($request->dsorderid, 2003, $message);//失败
            if (date('Y-m-d') == date('Y-m-d', $consume->consume_time)) {
                $plan->update(['repayment_plan_status' => 1005, 'update_plan_time' => time()]);
            }
            //发送消费失败消息提醒
            $userOwn = $this->userRepository->byId($plan->user_id);
            $creditCard = $this->userCreditCardRepository->isExistUserBankNo($plan->user_id, $consume->bank_no);
            $this->messageService->messageTemplate(16, $userOwn->invited_code, $creditCard->bank_no, $creditCard->bank_description,
                $consume->consume_money, '', '', $consume->repayment_plan_id, '', '');
        }
        $errorData = [
            'order_no' => $request->dsorderid,
            'error_code' => $request->status,
            'error_msg' => json_encode($request->all()),
            'api_type' => 18
        ];
        $this->threeApiErrorLogsRepository->create($errorData);
    }

    /**
     * 还款回调
     */
    public function repaymentNotify(Request $request)
    {
        Log::info('repaymentNotifyE:' . json_encode($request->all()));
        $this->repaymentProfitService->repaymentE($request);
    }

    /**
     * 提现回调
     */
    public function withdrawNotify(Request $request)
    {
        Log::info('withdrawNotifyE:' . json_encode($request->all()));
        $withdraw = $this->repaymentAccountWithdrawRepository->byWithdrawNo($request->dsorderid);
        //更新还款提现状态
        if ($request->status == '00') {
            $this->repaymentAccountWithdrawRepository->updateStatusByWithdrawNo($request->dsorderid, 4002);//成功
            echo 'SUCCESS';
            //提现成功失败消息提醒
            $userOwn = $this->userRepository->byId($withdraw->user_id);
            $this->messageService->messageTemplate(18, $userOwn->invited_code, $withdraw->bank_no, $withdraw->bank_description,
                $withdraw->withdraw_money, '', '', 0, '', '');
        } else {
            $this->repaymentAccountWithdrawRepository->updateStatusByWithdrawNo($request->dsorderid, 4003);//失败
        }
        $errorData = [
            'order_no' => $request->dsorderid,
            'error_code' => $request->status,
            'error_msg' => json_encode($request->all()),
            'api_type' => 28
        ];
        $this->threeApiErrorLogsRepository->create($errorData);
    }

    /**
     * 收银跳转url
     */
    public function quickMoneyReturn(Request $request)
    {
        Log::info('quickMoneyReturnE:' . json_encode($request->all()));
        $userPassageRegisterRepository = new CollectMoneyBillRepository();
        //如果是未支付
        $isSuccess = $userPassageRegisterRepository->findByBillSn($request->orderNo);
        if (!empty($isSuccess) && 6000 == $isSuccess->collect_money_status) {
            $userPassageRegisterRepository->updateBillStatus($request->orderNo, 6001);//处理中
            $errorData = [
                'order_no' => $request->orderNo,
                'error_code' => 6001,
                'error_msg' => '交易处理中',
                'api_type' => 23
            ];
            $this->threeApiErrorLogsRepository->create($errorData);
        }
    }
    
    /**
     * 收银回调
     * {"sign":"d3f2772568c19279d9a0de012e480848","amount":"10.00","transtype":"18","merchno":"lmkj25se267f69f","status":"00",
     * "signType":"MD5","dsorderid":"A913257061602895","orderid":"2018091315580100100122589","paytime":"20180913155846"}
     */
    public function quickMoneyNotify(Request $request)
    {
        Log::info('quickMoneyNotifyE:' . json_encode($request->all()));
        $errorData = [
            'order_no' => $request->dsorderid,
            'error_code' => $request->status,
            'error_msg' => json_encode($request->all()),
            'api_type' => 24
        ];
        $this->threeApiErrorLogsRepository->create($errorData);
        $this->collectMoneyProfitService->quickMoneyNotify($request);
    }
}